import { getLoader } from '../utils'
import { Alias, RollupAliasOptions } from '@rollup/plugin-alias'
import { isArray, isObject } from 'node-aux'

export const transformAlias = function (aliasObj: RollupAliasOptions['entries']) {
    let aliasList: Alias[] = []
    if (isObject(aliasObj)) {
        Object.keys(aliasObj).forEach((key) => {
            aliasList.push({
                find: key,
                replacement: aliasObj[key]
            })
        })
    }
    if (isArray(aliasObj)) {
        aliasList = [...aliasObj]
    }
    return aliasList
}

export const aliasPlugin = function (aliasObj: RollupAliasOptions['entries']) {
    const alias = getLoader<RollupAliasOptions>('@rollup/plugin-alias')

    const aliasList: Alias[] = transformAlias(aliasObj)

    return alias({
        entries: aliasList
    })
}
